<template>
    <el-form :model="form" label-width="160px">
        <el-form-item label="" v-if="form.identify == '企业用户'">
            <span>注意：企业用户提交注册申请后需要等待审核</span>
        </el-form-item>
        <el-form-item label="用户名" required>
            <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" required placeholder="请输入6位密码">
            <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item label="确认密码" required placeholder="请确认6位密码">
            <el-input v-model="form.repassword" type="password" />
        </el-form-item>
        <el-form-item label="学历" required  v-if="form.identify == '个人用户'">
            <el-input v-model="form.remark" type="text" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" required  v-if="form.identify == '企业用户'">
            <el-input v-model="form.code1" type="text" />
        </el-form-item>
        <el-form-item label="工商注册号" required  v-if="form.identify == '企业用户'">
            <el-input v-model="form.code2" type="text" />
        </el-form-item>
        <el-form-item label="经营状况" required  v-if="form.identify == '企业用户'">
            <el-radio-group v-model="form.state">
                <el-radio-button label="良好" />
                <el-radio-button label="一般" />
            </el-radio-group>
        </el-form-item>
        <el-form-item label="选择注册身份">
            <el-radio-group v-model="form.identify">
                <el-radio-button label="个人用户" />
                <el-radio-button label="企业用户" />
            </el-radio-group>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
            <el-button @click="$emit('close')">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus';
import { reactive, defineEmits } from 'vue';
import request from '@/api';
const emit = defineEmits(['close']);
const form = reactive({
    username: '',
    password: '',
    repassword: '',
    identify: '个人用户',
    remark: '',
    code1:'',
    code2:'',
    state:''
});

const onSubmit = async () => {
    if (form.username.trim() == '') {
        ElMessage.error('用户名不能为空');
        return;
    }
    if (form.password.trim() == '' || form.password.trim().length < 6) {
        ElMessage.error('密码不合格');
        return;
    }
    if (form.password != form.repassword) {
        ElMessage.error('两次输入密码不一致');
        return;
    }
    if(form.identify == '个人用户'){
        if (form.remark.trim() == '') {
            ElMessage.error('学历不能为空');
            return;
        }
       
    }
    if(form.identify == '企业用户'){
        if (form.code1.trim() == '') {
            ElMessage.error('统一社会信用代码不能为空');
            return;
        }
        if (form.code2.trim() == '') {
            ElMessage.error('工商注册号不能为空');
            return;
        }
        if (form.state.trim() == '') {
            ElMessage.error('经营状况不能为空');
            return;
        }
    }
    let res = await request({
        url: '/register',
        method: 'post',
        data: {
            username: form.username,
            password: form.password,
            type: form.identify == '个人用户' ? 1 : 2,
            remark: form.identify == '个人用户'?form.remark:'-',
            code1:form.identify == '企业用户'?form.code1:'-',
            code2:form.identify == '企业用户'?form.code2:'-',
            state:form.identify == '企业用户'?form.state:'-'

        },
    });
    if (res.success) {
        ElMessage.success('注册成功');
        emit('close');
    } else {
        ElMessage.error(res.msg);
    }
};
</script>
